(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteSystemConfig.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'storageService',
			'systemConfigService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, systemConfigService) {
		var ctrl = this, siteSystemConfigInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.siteSystemConfig = {};
		ctrl.dyDatas =[];
		ctrl.init = function() {
			var params = {
					siteCode : siteSystemConfigInfo.siteCode
			};
			ctrl.tableData = ctrl.tableData || [];
			systemConfigService.initSiteDynaParm($scope, ctrl, gridService);
			systemConfigService.getSiteServiceCode(ctrl).then(function(serviceCodeOptions){
				authService.create(params).then(function(response) {
					ctrl.siteSystemConfig = response.data || siteSystemConfigInfo;
					ctrl.siteSystemConfig.siteCode = storageService.getUserInfo().siteCode;
					ctrl.siteSystemConfig.serviceCode = serviceCodeOptions[0].value;
					ctrl.siteSystemConfig.dynamicParams = ctrl.siteSystemConfig.dynamicParams || {};
					ctrl.isInitialized = "Y";
				});
			});
		};
		
		ctrl.getSiteDynaparm = function(){
			if(ctrl.isInitialized == "Y"){
        	    systemConfigService.getSiteDynaparmTwo(ctrl,ctrl.siteSystemConfig);
			}else{
				ctrl.isInitialized = "N";
			}
        };
		
		ctrl.createSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.siteSystemConfig.taskId) {
				var a=ctrl.tableData;
				angular.forEach(ctrl.siteSystemConfig.data, function(serviceEntity) {
					angular.forEach(serviceEntity, function(entity, index) {
						if (entity.dynamicParams[entity.name]) {
							ctrl.siteSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
						}
					});
				});
				return authService.createContinueSave(ctrl.siteSystemConfig).then(saveSuccessful, saveFailed);
			}else {
				var a=ctrl.tableData;
				angular.forEach(ctrl.siteSystemConfig.data, function(serviceEntity) {
					angular.forEach(serviceEntity, function(entity, index) {
						if (entity.dynamicParams[entity.name]) {
							ctrl.siteSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
						}
					});
				});
				return authService.createSave(ctrl.siteSystemConfig).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.siteSystemConfig.taskId) {
				angular.forEach(ctrl.siteSystemConfig.data, function(serviceEntity) {
					angular.forEach(serviceEntity, function(entity, index) {
						if (entity.dynamicParams[entity.name]) {
							ctrl.siteSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
						}
					});
				});
				return authService.createContinueSubmit(ctrl.siteSystemConfig).then(submitSuccessful, submitFailed);
			}  else {
				angular.forEach(ctrl.siteSystemConfig.data, function(serviceEntity) {
					angular.forEach(serviceEntity, function(entity, index) {
						if (entity.dynamicParams[entity.name]) {
							ctrl.siteSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
						}
					});
				});
				return authService.createSubmit(ctrl.siteSystemConfig).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.siteSystemConfig.create');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				},
				{
					'name' : 'title.infobar.siteSystemConfig.create',
					'state' : 'cda.siteSystemConfig.create',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});

			if (response.data && response.data.model) {
				ctrl.siteSystemConfig = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);